package org.heydr.common.util;

import java.io.File;
import java.io.IOException;
import org.htmlcleaner.*;

public class HTMLCleanUtil {

	public static String clean(String orgPage) {
		String source = null;
		CleanerProperties props = new CleanerProperties();
		props.setAllowHtmlInsideAttributes(true);
		props.setOmitComments(true);
		props.setOmitDoctypeDeclaration(true);
		props.setOmitXmlDeclaration(true);
		props.setOmitDeprecatedTags(true);
		props.setPruneTags("link,base,style,meta,o:p");
		props.setAdvancedXmlEscape(true);
		HtmlCleaner cleaner = new HtmlCleaner(props);
		try {
			TagNode rootNode = cleaner.clean(orgPage);
			SimpleXmlSerializer xmlSerializer = new SimpleXmlSerializer(props);
			source = xmlSerializer.getAsString(rootNode);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return source;
	}

	public static void clean(String input, String output) {
		clean(input,output,"script,link,base,style,meta","utf-8");
	}
	
	public static void clean(String input, String output,String tags,String encode){
		CleanerProperties props = new CleanerProperties();
		props.setAllowHtmlInsideAttributes(true);
		props.setOmitComments(true);
		props.setOmitDoctypeDeclaration(true);
		props.setOmitXmlDeclaration(true);
		props.setOmitDeprecatedTags(true);
		props.setPruneTags(tags);
		props.setAdvancedXmlEscape(true);
		HtmlCleaner cleaner = new HtmlCleaner(props);
		try {
			TagNode rootNode = cleaner.clean(new File(input));
			SimpleXmlSerializer xmlSerializer = new SimpleXmlSerializer(props);
			xmlSerializer.writeToFile(rootNode, output, encode);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}
